package concurrent.threadSafe;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName OrderThread2.java
 * @Description 三个线程依次执行
 * @createTime 2021年04月14日 00:16:00
 */
public class OrderThread2 {
    static  volatile int ticket = 1;

    public static void main(String[] args) {
        Thread thread = new Thread(()->{
            while (true){
                if (ticket==1){
                    try {
                        Thread.sleep(100);
                        for (int i = 0; i <10 ; i++) {
                            System.out.println("a"+i);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket = 2;
                    return;
                }
            }
        });
        Thread thread2 = new Thread(()->{
            while (true){
                if (ticket==2){
                    try {
                        Thread.sleep(100);
                        for (int i = 0; i <10 ; i++) {
                            System.out.println("b"+i);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket = 3;
                    return;
                }
            }
        });
        Thread thread3 = new Thread(()->{
            while (true){
                if (ticket==3){
                    try {
                        Thread.sleep(100);
                        for (int i = 0; i <10 ; i++) {
                            System.out.println("c"+i);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket = 1;
                    return;
                }
            }
        });

        thread.start();
        thread2.start();
        thread3.start();
    }
}
